package activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.example.liuyujia.activity.R;

import util.CommonMethod;
import util.Constants;

/**
 * Created by liuyujia on 2015/3/30.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private EditText et_name;
    private EditText password;
    private Button btn_sure;
    private Button btn_reguster;
    private final  int REQUEST_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        et_name=(EditText)findViewById(R.id.signin_et_name);
        password=(EditText)findViewById(R.id.signin_et_password);
        btn_sure=(Button)findViewById(R.id.btn_sure);
        btn_sure.setOnClickListener(this);
        btn_reguster=(Button)findViewById(R.id.btn_register);
        btn_reguster.setOnClickListener(this);

        //leancloud init
        AVOSCloud.initialize(this, Constants.appid, Constants.appkey);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_sure:
              String name=et_name.getText().toString().trim();
              String pwd=password.getText().toString().trim();
                if (CommonMethod.isEmpty(name)){
                    CommonMethod.showToastMsg(LoginActivity.this, getString(R.string.username_empty));
                }
                else if(getUserName().length() != 11){
                    CommonMethod.showToastMsg(LoginActivity.this,getString(R.string.username_error));
                }
                else if (CommonMethod.isEmpty(pwd)){
                    CommonMethod.showToastMsg(LoginActivity.this,getString(R.string.password_empty));
                }else if(pwd.length() < 6|| pwd.length() > 18){
                    CommonMethod.showToastMsg(LoginActivity.this,getString(R.string.password_error));
                }else{
                    showProgressDialog("");
                    login();
                }
                break;
            case R.id.btn_register:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }
    }

    private void login(){
        AVUser.logInInBackground(getUserName(), getPassword(), new LogInCallback() {
            public void done(AVUser user, AVException e) {
                hideProgressDialog();
                if (user != null) {
                    // 登录成功
                    Intent intent=new Intent(LoginActivity.this,FragmentMainActivity.class);
                    startActivity(intent);
                    LoginActivity.this.finish();
                } else {
                    // 登录失败
                    if(Constants.errorMsg.containsKey(e.getCode())) {
                        CommonMethod.showToastMsg(LoginActivity.this, Constants.errorMsg.get(e.getCode()));
                    }
                    else
                        CommonMethod.showToastMsg(LoginActivity.this, getString(R.string.sign_in_failed));

                    return;
                }
            }
        });
    }

    private String getUserName(){
        return  et_name.getText().toString().trim();
    }

    private String getPassword(){
        return  password.getText().toString().trim();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_CODE){
            if(resultCode == RESULT_OK) {
                String user = data.getStringExtra("phone");
                if(user != null)
                    et_name.setText(user);

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
